What is @protobuf-ts/runtime-rpc?
@protobuf-ts/runtime-rpc is a TypeScript library that provides runtime support for Protocol Buffers (protobuf) and gRPC. It allows you to create and manage gRPC clients and servers, handle streaming, and perform unary calls with ease.
What are @protobuf-ts/runtime-rpc's main functionalities?
Unary Calls
This feature allows you to make unary gRPC calls. The code sample demonstrates how to set up a gRPC client and make a unary call to a service method.
const { GrpcWebFetchTransport } = require('@protobuf-ts/runtime-rpc');
const { MyServiceClient } = require('./my-service.client');
const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:8080' });
const client = new MyServiceClient(transport);
async function makeUnaryCall() {
const response = await client.myUnaryMethod({ myField: 'myValue' });
console.log(response);
}
makeUnaryCall();
Server Streaming
This feature allows you to handle server streaming gRPC calls. The code sample demonstrates how to set up a gRPC client and handle a server streaming method.
const { GrpcWebFetchTransport } = require('@protobuf-ts/runtime-rpc');
const { MyServiceClient } = require('./my-service.client');
const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:8080' });
const client = new MyServiceClient(transport);
async function handleServerStreaming() {
const stream = client.myServerStreamingMethod({ myField: 'myValue' });
for await (const response of stream.responses) {
console.log(response);
}
}
handleServerStreaming();
Client Streaming
This feature allows you to handle client streaming gRPC calls. The code sample demonstrates how to set up a gRPC client and handle a client streaming method.
const { GrpcWebFetchTransport } = require('@protobuf-ts/runtime-rpc');
const { MyServiceClient } = require('./my-service.client');
const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:8080' });
const client = new MyServiceClient(transport);
async function handleClientStreaming() {
const stream = client.myClientStreamingMethod();
stream.send({ myField: 'myValue1' });
stream.send({ myField: 'myValue2' });
stream.complete();
const response = await stream.response;
console.log(response);
}
handleClientStreaming();
Bidirectional Streaming
This feature allows you to handle bidirectional streaming gRPC calls. The code sample demonstrates how to set up a gRPC client and handle a bidirectional streaming method.
const { GrpcWebFetchTransport } = require('@protobuf-ts/runtime-rpc');
const { MyServiceClient } = require('./my-service.client');
const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:8080' });
const client = new MyServiceClient(transport);
async function handleBidirectionalStreaming() {
const stream = client.myBidirectionalStreamingMethod();
stream.send({ myField: 'myValue1' });
stream.send({ myField: 'myValue2' });
for await (const response of stream.responses) {
console.log(response);
}
}
handleBidirectionalStreaming();
Other packages similar to @protobuf-ts/runtime-rpc
@grpc/grpc-js
@grpc/grpc-js is a gRPC library for Node.js that provides similar functionalities for creating and managing gRPC clients and servers. It supports unary, server streaming, client streaming, and bidirectional streaming calls. Compared to @protobuf-ts/runtime-rpc, @grpc/grpc-js is more mature and widely used in the Node.js ecosystem.
grpc-web
grpc-web is a JavaScript library that enables gRPC clients to communicate with gRPC servers from web browsers. It provides similar functionalities for making unary and streaming calls. Compared to @protobuf-ts/runtime-rpc, grpc-web is specifically designed for web environments and may require additional setup for server-side support.
grpc
grpc is the original gRPC library for Node.js, providing comprehensive support for gRPC clients and servers. It supports all types of gRPC calls and is widely used in production environments. Compared to @protobuf-ts/runtime-rpc, grpc is more established but may have a steeper learning curve for TypeScript developers.
@protobuf-ts/runtime-rpc
Runtime library for RPC clients generated by protobuf-ts.
Install this plugin if you want to create your own RPC transport:
# with npm:
npm install @protobuf-ts/runtime-rpc
# with yarn:
yarn add @protobuf-ts/runtime-rpc
Or use one of the transports built with this package, for example Twirp via
@protobuf-ts/twirp-transport
or gRPC web via @protobuf-ts/grpcweb-transport.
To learn more about the types provided by this package, please read the
MANUAL.